# -*- coding: utf-8 -*-
import requests
import json, cv2
from base64 import b64decode
import numpy as np
import logging


def result_post():
    url = "http://8.134.53.114:8277/visual/uavAiAlarm/msgUpload"
    json_data = {}
    """南水直播检测报警测试"""
    logging.info("|INFO: post data to {}|".format(url))
    ret = requests.post(url=url, json=json_data)
    status_code = ret.status_code
    if status_code != 200:
        logging.error("|ERR: code {}, result_post fail|".format(status_code))


def start_streaming_l():
    json_data = {
        "id": 1,
        "tag": "task",
        "data": {
            "tag": "obj_detect",
            "data": {
                "tag": "GangHang",
                "data": {
                    "id": 2,
                    "SteamPull_url": "rtmp://218.94.155.86:19355/live/8UUXN2800A00K5",
                    # "SteamPull_url": "rtmp://218.94.155.86:19355/live/1581F8HGX252C00A00NN",
                    "Left_Heading": True,
                },
            },
        },
    }
    url = "http://0.0.0.0:2682/detect"

    request = requests.post(url=url, json=json_data)

    # with open("data/output/temp_file.json", "w") as f:
    #     jsondata = json.loads(request.text)
    #     json.dump(jsondata, f)

    print(request.text)

def start_streaming_r():
    json_data = {
        "id": 1,
        "tag": "task",
        "data": {
            "tag": "obj_detect",
            "data": {
                "tag": "GangHang",
                "data": {
                    "id": 1,
                    # "SteamPull_url": "rtmp://127.0.0.1/live/livestream_esdk",
                    "SteamPull_url": "rtmp://218.94.155.86:19355/live/1581F8HGX252C00A00NN",
                    "Left_Heading": False,
                },
            },
        },
    }
    url = "http://0.0.0.0:2682/detect"

    request = requests.post(url=url, json=json_data)

    # with open("data/output/temp_file.json", "w") as f:
    #     jsondata = json.loads(request.text)
    #     json.dump(jsondata, f)

    print(request.text)

def stop_streaming():
    json_data = {
        "id": 1,
        "tag": "task",
        "data": {
            "tag": "obj_detect",
            "data": {"tag": "pop", "data": None},
        },
    }
    url = "http://0.0.0.0:2682/detect"

    request = requests.post(url=url, json=json_data)

    # with open("data/output/temp_file.json", "w") as f:
    #     jsondata = json.loads(request.text)
    #     json.dump(jsondata, f)

    print(request.text)


if __name__ == "__main__":
    while True:
        user_input = input("请输入 a/d 执行开启推流,输入 s 执行停止推流:")
        if user_input == "a":
            start_streaming_l()
        elif user_input == "s":
            stop_streaming()
        elif user_input == "d":
            start_streaming_r()
        else:
            print("输入无效，请重新输入。")
